#ifndef __FOC_H
#define __FOC_H

#include "main.h"
#include "math.h"


// 初始化宏定义
#define init_sin {0,0,0}
#define mortor_init {0,0,0,24}
#define clark_init  {0,0}
#define pack_init {0,0}
#define repack_init {0,0}
#define svpwm_init {0,0,0,4250}
#define reclark_sin {0,0,0}
#define Angle_Gerate_Memory_Init {0.00005f,0,0,0,0} //开环电角度生成器宏
#define Motor_State_Params_Init {4,0.6,0.3,4.0,0.00005} //电机静态结构体初始化宏

#define Current_PID_Params {5,0.1,0,12.5f,-12.5f,0}
#define Speed_PID_Params {0.05,0.0,0,2.0f,-2.0f,0}  

#define LQF_init {0.5f,0}

#define SMO_init {0,0,0,0,0,0}
#define PLL_init {0.5f,0,0,0,0,0,0,0,0.00005f}


#define PI 3.1415926f




// FOC参数配置
#define A    12.0f      // 正弦波幅值
#define Fre  20.0f      // 正弦波频率(Hz)
#define Amp  1000.0f    // PWM幅值



// 三相正弦波数据结构
typedef struct{
	
	float Phase_A;
	float Phase_B;
	float Phase_C;
	
}SineWave_t;


// 电机实时参数结构
typedef struct{
	float Current_A;
	float Current_B;
	float Current_C;
	
	float DC_Voltage;	
	
	float M_theta;
	float Speed;
	
	float E_angle;
	
}Motor_RealTimeParams;


// 电机静态参数
typedef struct
{
	float Motor_Poler; //电机极对数
	float Motor_Rs;    //电机相电阻
	float Motor_Ls;    //电机相电感
	float Motor_Flux;  //永磁体磁链
	float Motir_Ts;    //控制周期
}Motor_State_Params;

// Clarke变换结构
typedef struct{
	float current_alpha;
	float current_beta;
}Clarke_Transform;




// Park变换结构
typedef struct{
	float current_D;
	float current_Q;
}Park_Transform;
	



// 逆Park变换结构	
typedef struct{
	float Voltage_alpha;
	float Voltage_beta;
}InversePark_Transform;	




// 逆Clarke变换结构	
typedef struct{
	float Voltage_A;
	float Voltage_B;
	float Voltage_C;
}InverseClarke_Transform;




// SVPWM结构	
typedef struct{
	float A_period;
	float B_period;
	float C_period;
	
	float Tpwm;
}SVPWM;	
	
	
	
	
typedef struct{
	float  Sample_Period;     //采样周期
	float  Eelectrical_Angle; //生成的电角度
	float  Mechanical_Freqnence; //开环机械角频率
	float  Mechanical_Angle;    //生成的机械角度
	float  open_Speed_Freqnence; //开设转速频率  弧度/s
}Angle_Gerate_Memory;
	
	


	//PI控制器结构体
typedef struct {
    float Kp;           // 比例系数
    float Ki;           // 积分系数 
	float IntegralSum;
    float OutMax;       // 输出最大值限幅
    float OutMin;       // 输出最小值限幅
	float Output;
} PI_Controller;

/*        低通滤波器        */
typedef struct {
	float LQFK;
	float OUTPUT;
}LQF;


/*          滑膜观测器结构体            */
typedef struct {
	//滑膜控制参数
	float SMO_K;   //滑膜增益
	float E_LQF;   //反电动势低通滤波系数
	
	//电流估算值
	float I_Alpha_Hat;  //α轴估算电流
	float I_Beta_Hat;   //β轴估算电流值
	
	//滤波后的反电动势
	float E_Alpha_Filter;   //Alpha轴反电动势
	float E_Beta_Filter;    //Beta轴反电动势
	
}SMO_observe;
//锁相环结构体
typedef struct {
	float PLL_LQF;	     //滤波函数
	float We_Filter;     //滤波后的角速度
	float Error_Theta;   //角度误差
	float Theta_Hat_Pervious; //上个时刻估算出来的电角度
	float Theta_Hat;          //估算到的电角度
	float We;            //估算到的电 角速度
	float PI_OutPut;     //PI控制器输出
	float Integral_Term; //积分项
	float Ts;            //采样周期
	
}PLL;	
	


//非线性磁链观测器结构体
typedef struct
{
  float X_Alpha;   //定子磁链在Alpha轴上的分量v  
  float X_Beta;    //定子磁链在Beta轴上的分量
  float Eta_X_Alpha; //转子磁链在Alpha轴上的分量
  float Eta_X_Beta;  //转子磁链在Beta轴上的分量
  float Eta_x_s;     //转子磁链合成值
  float Y_Alpha;     //反电动势在Alpha轴上的分量
  float Y_Beta;      //反电动势在Beta轴上的分量
  float Flux_Error;  //磁链误差值
  float Error_Mpy_Gain; //磁链误差值x增益
  float Inter_Input_Alpha;  //积分器输入Alpha轴上的分量
  float Inter_Input_Beta;   //积分器输入Beta轴上的分量
  float Motor_Rs;      //电机相电阻
  float Motor_Ld;      //d轴电感
  float Motor_Lq;      //q轴电感
  float Ts;            //控制周期
  float Motor_Flux;    //永磁体磁链
  float Flux_Active_Part;  //有效磁链分量
  float Active_Flux;    //有效磁链
  float Observer_Gain;  //观测器增益
  float PLL_Error;      //锁相环输入误差
  float PLL_Kp;         //锁相环比例
  float PLL_Ki;         //积分项目
  float PLL_Out_Limt;   //输出限幅度
  float Flux_Alpha;    //转子磁链alpha轴分量
  float Flux_Beta;    //转子磁链Beta轴分量
  float Flux_s;       //转子磁链合成值
  float Flux_Theta;            // 转子电角度（rad）
  float Flux_Omega;           //转子电角速度（rad/s）
  float Flux_Mechanical_Speed; //转子机械转速	
  float PI_Inter;         //PI控制器积分项
  float PI_Inter_Limit;         //PI控制器积分限幅
  float PI_Out_Limit;         //PI控制器输出限幅
  float LQF_K;                //速度滤波系数
  float Filter_Speed;         //滤波后的转速输出
  
}Flux_Memory;





void ThreePhaseSineWave_Generate(SineWave_t *sineWave, float *CHUXIANG);
void ClarkeTransform(Clarke_Transform *clarke , Motor_RealTimeParams *motorParams);
void ParkTransform(Clarke_Transform *clarke , Motor_RealTimeParams *motorParams,Park_Transform *PARK);
void InverseParkTransform_init(float UD, float UQ, Motor_RealTimeParams *motorParams , InversePark_Transform *inversePark);
void InverseClarkeTransform_init(InverseClarke_Transform *inverseClarke , InversePark_Transform *inversePark);
void SpaceVectorPWM(InverseClarke_Transform *inverseClarke , SVPWM *Svpwm, Motor_RealTimeParams *motorParams);
//开环角度生成函数  
void angle_Generate(Angle_Gerate_Memory *Angle_Memory,Motor_State_Params *M_Memory);

void PI_Calculate(PI_Controller *pid, float setpoint, float actual);
void LQF_Filter(LQF *p , float inout);

float Saturation(float Val , float Phi);
void PLL_Oberserver(PLL *Pll , float E_Alpha , float E_Beta , float Kp , float Ki , float We_limit, Motor_State_Params *motor);
void SMO_Oberserve(SMO_observe *SMO,float U_Alpha , float U_Beta , float I_Alpha , float I_Beta , Motor_State_Params *motor);

//初始化磁链观测器
void Init_Flux_Observe(Flux_Memory *Flux_Memory);
//非线性磁链观测器
void Flux_PLL_Observe(Flux_Memory *Flux_Memory,float I_Alpha,float I_Beta,float U_Alpha,float U_Beta,float ID);


#endif

